package com.niji.littly_donkey_android.adapter;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.*;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.niji.littly_donkey_android.R;
import com.niji.littly_donkey_android.activity.IndexActivity;
import com.niji.littly_donkey_android.bean.Follow;
import com.niji.littly_donkey_android.bean.Video;
import com.niji.littly_donkey_android.util.NetworkGlideImageLoader;
import com.niji.littly_donkey_android.util.VideoUtil;
import com.niji.littly_donkey_android.view.CircleImageView;

import java.util.ArrayList;

public class IndexVideoAdapter extends ArrayAdapter<Video> {


    private static final String TAG = "IndexVideoAdapter";

    private Context context;

    public IndexVideoAdapter(Context context, ArrayList<Video> members)
    {
        super(context,0, members);
        this.context = context;
    }
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Video video = getItem(position);

        if(convertView == null)
        {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_video, parent, false);
        }

        VideoView item_index_video = convertView.findViewById(R.id.item_index_video);
//        CircleImageView item_index_music = convertView.findViewById(R.id.item_index_music);
//        CircleImageView item_img_head = convertView.findViewById(R.id.item_img_head);
//        TextView item_text_heartNum = convertView.findViewById(R.id.item_text_heartNum);
//        TextView item_text_commentNum = convertView.findViewById(R.id.item_text_commentNum);
//        TextView item_text_shareNum = convertView.findViewById(R.id.item_text_shareNum);
//        TextView item_text_introduce = convertView.findViewById(R.id.item_text_introduce);
//        TextView item_text_commission = convertView.findViewById(R.id.item_text_commission);
//        TextView item_text_name = convertView.findViewById(R.id.item_text_name);
//        TextView item_index_video_id = convertView.findViewById(R.id.item_index_video_id);
//        ImageView item_index_video_cover = convertView.findViewById(R.id.item_index_video_cover);
//        ImageView item_text_commission_logo = convertView.findViewById(R.id.item_text_commission_logo);
//        Button item_img_share = convertView.findViewById(R.id.item_img_share);
//        Button item_img_comment = convertView.findViewById(R.id.item_img_comment);
//        Button item_img_heart = convertView.findViewById(R.id.item_img_heart);
//        Button item_index_want_go = convertView.findViewById(R.id.item_index_want_go);
//        //thumb.setImageBitmap(BitMap.getInstance().returnBitMap(u.getThumb()));
//        //android4.0后要访问网络资源需要单开线程，加入这段就不会报错了
//        NetworkGlideImageLoader.getInstance().displayImage((Activity) context,video.getAvatar(),item_index_music,0,0);
//        NetworkGlideImageLoader.getInstance().displayImage((Activity) context,video.getAvatar(),item_img_head,0,0);
//        NetworkGlideImageLoader.getInstance().displayImage((Activity) context,video.getCover(),item_index_video_cover,0,0);
//        item_index_video_id.setText("" + video.getId());
//        item_text_heartNum.setText("" + video.getLikes());
//        item_text_commentNum.setText("" + video.getComments());
//        item_text_shareNum.setText("" + video.getShares());
//        item_text_introduce.setText(video.getTitle());
//        item_text_name.setText("@" + video.getNickname());
        Uri uri = Uri.parse(video.getUrl());
        item_index_video.setVideoURI(uri);
        item_index_video.start();
//        item_index_video_cover.setVisibility(View.GONE);
        item_index_video.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                item_index_video.stopPlayback(); //播放异常，则停止播放，防止弹窗使界面阻塞
                Toast.makeText(context,"不支持当前格式",Toast.LENGTH_SHORT).show();
                return true;
            }
        });

//        if (video.isIs_likes()){
//            item_img_heart.setBackground(convertView.getResources().getDrawable(R.mipmap.smallheart));
//        }else {
//            item_img_heart.setBackground(convertView.getResources().getDrawable(R.mipmap.praisetoicon));
//        }
//        if (video.getCommission() == 1){
//            item_text_commission.setVisibility(View.INVISIBLE);
//            item_text_commission_logo.setVisibility(View.INVISIBLE);
//        }else if(video.getCommission() == 2){
//            item_text_commission.setVisibility(View.VISIBLE);
//            item_text_commission_logo.setVisibility(View.VISIBLE);
//        }
//
//        Animation animation = AnimationUtils.loadAnimation(context, R.anim.img_range);
//        LinearInterpolator linearInterpolator = new LinearInterpolator();
//        animation.setInterpolator(linearInterpolator);
//        item_index_music.startAnimation(animation);
        return convertView;
    }




}



